package de.LooLo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.Spinner;

/**
 * Activity for Main Menu
 * @author Kevin Seppelt, Lukas Siedel 
 *
 */
public class LooLoActivity extends Activity {

	/**
	 * Button to the map
	 */
	private ImageButton mapButton;
	/**
	 * Checkbox to disable showing Main Menu on Appstart
	 */
	private CheckBox checkbox;
	/**
	 * Checkbox for Wheelchair Toilet Filter
	 */
	private CheckBox checkbox_wheel;
	/**
	 * Loo Loading Range Selector
	 */
	private Spinner rangeSpinner;

	public DatabaseManagement dbs = DatabaseManagement.getInstance();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		dbs.DataHelper(this);		
		checkbox = (CheckBox) findViewById(R.id.map_start_checkbox);
		checkbox_wheel = (CheckBox) findViewById(R.id.wheelchair_checkbox);
		mapButton = (ImageButton) findViewById(R.id.button_to_map);
		rangeSpinner = (Spinner) findViewById(R.id.range_spinner);
		ArrayAdapter<CharSequence> range_adapter = ArrayAdapter.createFromResource(
				this, R.array.loading_range,
				android.R.layout.simple_spinner_item);
		range_adapter.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		rangeSpinner.setAdapter(range_adapter);
		rangeSpinner.setSelection((Integer.parseInt(
						DatabaseManagement.getInstance().selectFirstRange())/50 - 1));
		rangeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String range = (String) rangeSpinner.getSelectedItem();
				range = range.replace("m", "");
				dbs.updateRange(range);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				dbs.updateRange("200");
				
				
			}
		});
		checkbox.setChecked(dbs.selectFirstMenu());
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)dbs.insertMenu();
				else dbs.deleteAllMenu();
			}
		});
		checkbox_wheel.setChecked(dbs.selectFirstWheel());
		checkbox_wheel.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)dbs.insertWheel();
				else dbs.deleteAllWheel();
			}
		});

		mapButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent uploadIntent;
				uploadIntent = new Intent(LooLoActivity.this,
						MapViewActivity.class);
				
				startActivity(uploadIntent);
				finish();
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.close_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.leave_running:
			finish();
			return true;

		case R.id.kill:
			System.exit(0);
			return true;

		default:
			return true;

		}
	}

	@Override
	public void onBackPressed() {
		openOptionsMenu();
	}

}